﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMS.SubCipherSolver.Views;
using AMS.SubCipherSolver.Models;
using System.Windows.Forms;
using System.IO;
using AMS.SubCipherSolver.Args;

namespace AMS.SubCipherSolver.Controllers
{
    public class SolverController
    {
        SolverForm _form;
        SolverModel _model;
        OpenFileDialog _openFileDialog;
        SaveFileDialog _saveDialog;

        public event EventHandler<SolutionEventArgs> SolutionUpdated
        {
            add { _model.SolutionUpdated += value; }
            remove { _model.SolutionUpdated -= value; }
        }

        public event EventHandler<CipherEventArgs> CipherUpdated
        {
            add { _model.CipherUpdated += value; }
            remove { _model.CipherUpdated -= value; }
        }

        public event EventHandler SolutionSaved
        {
            add { _model.SolutionSaved += value; }
            remove { _model.SolutionSaved -= value; }
        }

        public event EventHandler KeySaved
        {
            add { _model.KeySaved += value; }
            remove { _model.KeySaved -= value; }
        }

        public event EventHandler<KeyLoadedEventArgs> KeyLoaded
        {
            add { _model.KeyLoaded += value; }
            remove { _model.KeyLoaded -= value; }
        }

        public event EventHandler<CipherEventArgs> CipherAnalyzedForDoubleChars
        {
            add { _model.CipherAnalyzedForDoubleChars += value; }
            remove { _model.CipherAnalyzedForDoubleChars -= value; }
        }

        public event EventHandler<CipherEventArgs> CipherAnalyzedForCharsInWordsCount
        {
            add { _model.CipherAnalyzedForCharsInWordsCount += value; }
            remove { _model.CipherAnalyzedForCharsInWordsCount -= value; }
        }

        public event EventHandler<CipherPatternEventArgs> CipherAnalyzedForCommonWordPatterns
        {
            add { _model.CipherAnalyzedForCommonWordPatterns += value; }
            remove { _model.CipherAnalyzedForCommonWordPatterns -= value; }
        }
        
        public event EventHandler<CipherWordsEventArgs> CipherAnalyzedForShortWords
        {
            add { _model.CipherAnalyzedForShortWords += value; }
            remove { _model.CipherAnalyzedForShortWords -= value; }
        }
        
        public SolverController()
        {
            this._model = new SolverModel();
            this._form = new SolverForm(this);

            this._openFileDialog = new OpenFileDialog();
            this._openFileDialog.Filter = "Text Files|*.txt|All Files|*.*";
            this._openFileDialog.FilterIndex = 0;

            this._saveDialog = new SaveFileDialog();
            this._saveDialog.Filter = "Text Files|*.txt|All Files|*.*";
            this._saveDialog.FilterIndex = 0;
        }

        public void Execute()
        {
            this._form.ShowDialog();
        }

        public void CharacterSelectionChanged(char cipherChar, char solnChar)
        {
            _model.SetCharacter(cipherChar, solnChar);
        }

        public void LoadCipher()
        {
            if (this._openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo file = new FileInfo(_openFileDialog.FileName);
                _model.LoadCipher(file);
            }
        }

        public void LoadKey()
        {
            if (this._openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo file = new FileInfo(_openFileDialog.FileName);
                _model.LoadKey(file);
            }
        }

        public void SaveKey()
        {
            if (this._saveDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo keyFile = new FileInfo(_saveDialog.FileName);
                try
                {
                    _model.SaveKey(keyFile, _form.LettersValid, _form.DigitsValid, _form.PunctuationValid);
                }
                catch (FileLoadException)
                {
                    DialogResult result = MessageBox.Show("File exists.  Overwrite?", "File", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                        _model.SaveKey(keyFile, _form.LettersValid, _form.DigitsValid, _form.PunctuationValid, true);
                }
            }
        }

        public void SaveSolution()
        {
            if (this._saveDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo solnFile = new FileInfo(_saveDialog.FileName);
                try
                {
                    _model.SaveSolution(solnFile);
                }
                catch (FileLoadException)
                {
                    DialogResult result = MessageBox.Show("File exists.  Overwrite?", "File", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == DialogResult.Yes)
                        _model.SaveSolution(solnFile, true);
                }
            }
        }
    }
}
